/***************************************************************************
 *   Copyright (C) 2008/2009 by Philipp Müller                             *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#ifndef QTVOMP_H
#define QTVOMP_H

#include <QMainWindow>
#include <QButtonGroup>
#include <QStackedWidget>

#include "settings.h"
#include "mediawidgetitem.h"
#include "recordingswidgetitem.h"
#include "channellist.h"
#include "channellistmodel.h"
#include "timerlistmodel.h"
#include "vdrthread.h"
#include "widgetplayvideo.h"
#include "widgetoptions.h"
#include "widgetepg.h"
#include "widgettimers.h"
#include "widgetepg.h"
#include "widgetchannellist.h"


class QLabel;
class QPushButton;
class QListView;
class QHostAddress;

class MainWindow : public QMainWindow
{
        Q_OBJECT

    public:
        /** Menu widgets */
        enum Menu
        {
            WIDGET_MENU = 0,
            WIDGET_CHANNELS,
            WIDGET_RECORDINGS,
            WIDGET_TIMERS,
            WIDGET_MEDIA,
            WIDGET_PLAYVIDEO,
            WIDGET_EPG,
            WIDGET_OPTIONS
        };

        MainWindow( QHostAddress server, QWidget *parent = 0 );
        ~MainWindow();

        Settings* settings() { return m_settings; };

        QSize sizeHint() const { return QSize( 752, 576 ); };

        void closeEvent ( QCloseEvent * event );

        VDR* vdr() { return m_vdr; };

        ChannelList * channelList() { return &m_channelList; };

        ChannelListModel* channelListModel() { return m_channelListModel; };

        QItemSelectionModel* channelListSelectionModel() { return m_channelListSelectionModel; };

        TimerListModel* timerListModel() { return m_timerListModel; };

        QList<TimerEntry> * timerList() { return &m_timerList; };

        QItemSelectionModel* timerListSelectionModel() { return m_timerListSelectionModel; };

        QStackedWidget* centralWidget() { return m_centralWidget; };

        QTreeWidget* recordingsList() { return m_recordingsList; };

        QTreeWidget* mediaList() { return m_mediaList; };

        QDateTime serverTime();

        QTimer* serverTimer() { return m_timer; };


    protected:
        void keyPressEvent( QKeyEvent *event );
        void moveEvent ( QMoveEvent * event );
        void resizeEvent ( QResizeEvent * event );
        void hideEvent ( QHideEvent * event );
        void showEvent ( QShowEvent * event );

    signals:
        void setVDRServerAddress( QHostAddress vdr );
        void getChannelList( quint32 );
        void getRecordingsList();
        void getTimerList();
        void requestMediaList( const QString& dir );
        void requestOpenRecording( const QString& file );

    private slots:
        void displayMenu();
        void displayMenuTime();
        void displayChannelList( bool loadChannels = true );
        void displayChannelsRadio( bool loadChannels = true );
        void displayTimers();
        void displayRecordings( bool loadRecordings = true );
        void displayMediaList( bool loadMedia = true );
        void displayMediaList( QTreeWidgetItem * item );
        void displayOptions();
        void displayEpg();
        void displayRecordingInformation();

        void updateChannels( const ChannelList & channelList );
        void updateRecordings( const QList<RecordingsEntry> & recordingsList );
        void updateMediaList( const QList<MediaEntry> & mediaList );
        void updateTimers( const QList<TimerEntry> & timerList );
        void checkSetTimerResult( int result );
        void checkDeleteTimerResult( int result );
        void checkEditTimerResult( int result );

    private:
        QHostAddress searchForServer( VDRThread * thread );
        void initMenuWidget();
        void initChannelListWidget();
        void initRecordingsWidget();
        void initMediaWidget();
        void initOptionsWidget();
        void initTimersWidget();
        void initEpgWidget();

        //Recursive helperfunction
        void addRecordingsWidgetItem( const QString & text, const QString & fileName,
                                      int start, QDateTime dateTime, RecordingsWidgetItem * parentItem );

        QStackedWidget*      m_centralWidget;
        QWidget*             m_menuWidget;
        QWidget*             m_mediaWidget;
        QWidget*             m_recordingsWidget;
        WidgetEpg*           m_epgWidget;
        WidgetTimers*        m_timersWidget;
        WidgetOptions*       m_optionsWidget;
        WidgetPlayVideo*     m_widgetPlayVideo;
        WidgetChannelList*   m_channelListWidget;

        ChannelListModel*    m_channelListModel;
        QItemSelectionModel* m_channelListSelectionModel;
        TimerListModel *     m_timerListModel;
        QItemSelectionModel* m_timerListSelectionModel;
        ChannelList          m_channelList;
        QList<TimerEntry>    m_timerList;

        QTreeWidget*         m_mediaList;
        MediaWidgetItem*     m_mediaListItem;

        QTreeWidget*          m_recordingsList;
        RecordingsWidgetItem* m_recordingsItem;

        QLabel* m_menuTimeLabel;

        QTimer* m_timer;

        VDRThread m_vdrThread;
        VDR *     m_vdr;

        qint32    m_serverTimeOffset;

        Settings* m_settings;
};

#endif
